package show.hkl.tools.datageneration.batchproce.writer;

import cn.hutool.core.util.CreditCodeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import org.springframework.stereotype.Service;
import show.hkl.tools.datageneration.batchproce.model.entity.BookEntity;
import show.hkl.tools.datageneration.batchproce.model.entity.BookStoreEntity;
import show.hkl.tools.datageneration.batchproce.model.entity.BookStoreMapEntity;
import show.hkl.tools.datageneration.batchproce.service.IBookService;
import show.hkl.tools.datageneration.batchproce.service.IBookStoreMapService;
import show.hkl.tools.datageneration.batchproce.service.IBookStoreService;

import javax.annotation.Resource;

@Service
public class Writer {

    @Resource
    private IBookService bookService;

    @Resource
    private IBookStoreService bookStoreService;

    @Resource
    private IBookStoreMapService bookStoreMapService;


    private BookEntity bookEntity = new BookEntity();
    private BookStoreEntity bookStoreEntity = new BookStoreEntity();
    private BookStoreMapEntity bookStoreMapEntity = new BookStoreMapEntity();

    public void generation() {
        System.out.println(Thread.currentThread().getName());
        // 生成 book
        bookEntity.setInventory(RandomUtil.randomInt(1, 10));
        bookEntity.setBookName(get32BitChar());
        bookEntity.setBookIndex(get32BitChar());
        bookEntity.setIsbn(CreditCodeUtil.randomCreditCode());
        bookEntity.setId(null);
        bookService.save(bookEntity);

        bookStoreEntity.setBookCaseNo(get32BitChar());
        bookStoreEntity.setBookCaseName(get32BitChar());
        bookStoreEntity.setId(null);
        bookStoreService.save(bookStoreEntity);

        bookStoreMapEntity.setBookId(bookEntity.getId());
        bookStoreMapEntity.setBookCaseId(bookStoreEntity.getId());
        bookStoreMapEntity.setId(null);
        bookStoreMapService.save(bookStoreMapEntity);
    }

    /**
     * 获取32位
     *
     * @return
     */
    public static String get32BitChar() {
        return String.valueOf(SecureUtil.md5(String.valueOf(RandomUtil.randomInt(1, 10))));
    }

}
